﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
//new
using System.IO;

namespace JKZP
{
    public partial class addroad : Form
    {
        public addroad()
        {
            InitializeComponent();
        }

        private void addroad_Load(object sender, EventArgs e)
        {

        }

        private void button_save_Click(object sender, EventArgs e)
        {
            if (textBox_id.Text == "")
            {
                toolStripStatusLabel1.Text = "添加失败，路口编号不能为空";
                return;
            }
            if (textBox_id.Text.Length != 2)
            {
                toolStripStatusLabel1.Text = "添加失败，路口编号长度为2";
                return;
            }
            if (textBox_name.Text == "")
            {
                toolStripStatusLabel1.Text = "添加失败，路口名称不能为空";
                return;
            }
            if (textBox_gbdm.Text == "")
            {
                toolStripStatusLabel1.Text = "添加失败，国标代码不能为空";
                return;
            }
            //if (textBox_bmbh.Text == "")
            //{
            //    toolStripStatusLabel1.Text = "添加失败，部门编号不能为空";
            //    return;
            //}

            DataGridView dgv = ((RoadForm)(this.Owner)).dataGridView1;
            for (int i = 0; i < dgv.Rows.Count; i++)
            {
                DataGridViewRow r = dgv.Rows[i];
                if (textBox_id.Text == r.Cells["roadid"].Value.ToString())
                {
                    toolStripStatusLabel1.Text = "添加失败，编号已存在";
                    return;
                }
            }
            if (AddRoad(textBox_id.Text, textBox_name.Text,textBox_gbdm.Text,textBox_bmbh.Text) == true)
            {
                dgv.Rows.Add(new object[] { textBox_id.Text, textBox_name.Text, textBox_gbdm.Text, textBox_bmbh.Text });
                ((Form_Main)this.Owner.Owner).UpDateTree();

                if (PP.gISWebVersion == false)
                {
                    toolStripStatusLabel1.Text = "添加成功";
                }
                else
                {
                    if (PP.kdws.JKZP_UploadRoad(File.ReadAllText(PP.gRoadFile)) == true)
                    {
                        PP.ReLoad();
                        toolStripStatusLabel1.Text = "添加成功";
                    }
                    else
                    {
                        toolStripStatusLabel1.Text = "添加失败";
                    }
                }
            }
            else
            {
                toolStripStatusLabel1.Text = "添加失败";
            }
        }

        private void button_cancle_Click(object sender, EventArgs e)
        {
            Close();
        }

        private bool AddRoad(string roadid, string roadname,string gbdm,string bmbh)
        {
            //<?xml version="1.0" encoding="UTF-8"?>
            //<param>
            //<road roadid="01" gbdm="123456789" roadname="测试路口一">
            //    <camera ip="172.16.22.252" type="HK9121" direction="由东向西" workmode="" bz=""></camera>
            //    <camera ip="172.16.22.250" type="HK9151" direction="由南向北" workmode="" bz=""></camera>
            //</road>
            //</param>


            string xpath = "descendant::param";
            string[] key = new string[6];
            string[] value = new string[6];
            key[0] = "roadid";
            key[1] = "gbdm";
            key[2] = "roadname";
            key[3] = "bmbh";
            key[4] = "user";
            key[5] = "luduan";
            value[0] = roadid;
            value[1] = gbdm;
            value[2] = roadname;
            value[3] = bmbh;
            value[4] = PP.gUser.username + "-";
            value[5] = "";
            bool ans = PP.AddConfig(PP.gRoadFile, xpath, "road", "", key, value);
            if (ans == true)
            {
                PP.RoadParam road = new PP.RoadParam();
                road.LKID = roadid;
                road.LKMC = roadname;
                road.GBDM = gbdm;
                road.BMBH = bmbh;
                road.USER = PP.gUser.username + "-";
                road.LuDuan = "";
                road.JKCamList = new List<JKCamera>();
                if (PP.gRoadList != null) PP.gRoadList.Add(road);
            }
            return ans;
        }
    }
}
